/*
 * RandomFork.cc
 *
 *  Created on: 2011-05-09
 *      Author: Jagger
 */

#include "RandomFork.h"

Define_Module(RandomFork)

void RandomFork::handleMessage(cMessage* msg)
{
	int size = gateSize("out");
	int gateNum = intuniform(0, size - 1);

	// Uwzgledniamy opoznienie wynikajace z niedoskonalosci kanalu
	// wyjsciowego.
	cGate* out = gate("out", gateNum);
	cChannel* channel = out->getChannel();
	if(channel)
	{
		simtime_t finishTime = channel->getTransmissionFinishTime();
		simtime_t delay = std::max(finishTime - simTime(), SIMTIME_ZERO);

		sendDelayed(msg, delay, out);
	}
	else
		send(msg, out);
}
